package cn.bdqn.controller.questionBank;

import cn.bdqn.dto.LoginUser;
import cn.bdqn.dto.questionBank.subject.SubjectAccountTypeDTO;
import cn.bdqn.service.questionBank.AccountTypeService;
import cn.bdqn.utils.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/accountType")
public class AccountTypeController {

    @Autowired
    private AccountTypeService accountTypeService;

    /**
     * 判断还有没有当前科目类型的试卷和题目
     * @param accountTypeId
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/selectAccountTypeNumberOfSubjectAndTestPaper")
    public Json selectAccountTypeNumberOfSubjectAndTestPaper(Integer accountTypeId){
        //System.out.println(accountTypeId);
        return accountTypeService.selectAccountTypeNumberOfSubjectAndTestPaper(accountTypeId);
    }

    /**
     *  修改科目
     * @param subjectAccountTypeDTO
     * @param httpServletRequest
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @PutMapping("/updateAccountType")
    public Json updateAccountType(@RequestBody SubjectAccountTypeDTO subjectAccountTypeDTO, HttpServletRequest httpServletRequest){
        //System.out.println(subjectAccountTypeDTO);
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        System.out.println(userid);
        try {
            return accountTypeService.updateAccountType(subjectAccountTypeDTO,userid);
        } catch (Exception e) {
            return Json.fall("修改失败，报异常");
        }
    }

    /**
     * 添加科目
     * @param subjectAccountTypeDTO
     * @param httpServletRequest
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @PostMapping("/insertAccountType")
    public Json insertAccountType(@RequestBody SubjectAccountTypeDTO subjectAccountTypeDTO, HttpServletRequest httpServletRequest){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        try {
            return accountTypeService.insertAccountType(subjectAccountTypeDTO,userid);
        } catch (Exception e) {
            return Json.fall("添加失败，报异常");
        }
    }

    /**
     * 查询所有行业和科目信息
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @GetMapping("/selectIndustryAccountType")
    public Json selectIndustryAccountType(){
        return accountTypeService.selectIndustryAccountType();
    }
}
